home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / PickTest / IntersectTest.java.z / IntersectTest.java
Encoding:
Java Source  |  2003-08-08  |  7.1 KB  |  237 lines

  1. /*
  2.  *    @(#)IntersectTest.java 1.10 02/10/21 13:48:59
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.BorderLayout;
  42. import java.awt.event.*;
  43. import java.awt.GraphicsConfiguration;
  44. import com.sun.j3d.utils.applet.MainFrame;
  45. import com.sun.j3d.utils.universe.*;
  46. import javax.media.j3d.*;
  47. import javax.vecmath.*;
  48. import com.sun.j3d.utils.behaviors.keyboard.*;
  49. import com.sun.j3d.utils.picking.PickTool;
  50.  
  51. public class IntersectTest extends Applet {
  52.  
  53.   BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);
  54.  
  55.     private SimpleUniverse u = null;
  56.  
  57.   public BranchGroup createSceneGraph () {
  58.  
  59.     // Create the root of the branch graph
  60.     BranchGroup objRoot = new BranchGroup();
  61.  
  62.     // Set up the ambient light
  63.     Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
  64.     AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  65.     ambientLightNode.setInfluencingBounds(bounds);
  66.     objRoot.addChild(ambientLightNode);
  67.  
  68.     // Set up the directional lights
  69.     Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
  70.     Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);
  71.     Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
  72.     Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);
  73.  
  74.     DirectionalLight light1
  75.       = new DirectionalLight(light1Color, light1Direction);
  76.     light1.setInfluencingBounds(bounds);
  77.     objRoot.addChild(light1);
  78.  
  79.     DirectionalLight light2
  80.       = new DirectionalLight(light2Color, light2Direction);
  81.     light2.setInfluencingBounds(bounds);
  82.     objRoot.addChild(light2);
  83.  
  84.     Transform3D t3 = new Transform3D ();
  85.  
  86.     // Shapes
  87.     for (int x=0;x<3;x++) {
  88.       for (int y=0;y<3;y++) {
  89.     for (int z=0;z<3;z++) {
  90.       t3.setTranslation (new Vector3d(-4+x*4.0, -4+y*4.0, -20-z*4.0));
  91.       TransformGroup objTrans = new TransformGroup(t3);
  92.  
  93.       objRoot.addChild(objTrans);
  94.  
  95.       // Create a simple shape leaf node, add it to the scene graph.
  96.       GeometryArray geom = null;
  97.  
  98.       if (((x+y+z) % 2) == 0) {
  99.         geom = new RandomColorCube();
  100.       }
  101.       else {
  102.         geom = new RandomColorTetrahedron();
  103.       }
  104.  
  105.        Shape3D shape = new Shape3D(geom);
  106.  
  107.       // use the utility method to set the capabilities
  108.       PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
  109.  
  110.       objTrans.addChild(shape);
  111.     }
  112.       }
  113.     }
  114.  
  115.     // Lines
  116.     Point3f[] verts = {
  117.       new Point3f (-2.0f, 0.0f, 0.0f),new Point3f(2.0f, 0.0f, 0.0f)
  118.     };
  119.     Color3f grey = new Color3f (0.7f, 0.7f, 0.7f);
  120.     Color3f[] colors = {
  121.       grey, grey
  122.     };
  123.  
  124.     for (int y=0;y<5;y++) {
  125.       for (int z=0;z<5;z++) {
  126.     t3.setTranslation (new Vector3d(7.0, -4+y*2.0, -20.0-z*2.0));
  127.     TransformGroup objTrans = new TransformGroup(t3);
  128.  
  129.     objRoot.addChild(objTrans);
  130.  
  131.     LineArray la = new LineArray (verts.length, 
  132.                       LineArray.COORDINATES |
  133.                       LineArray.COLOR_3);
  134.     la.setCoordinates (0, verts);
  135.     la.setColors (0, colors);
  136.  
  137.  
  138.     Shape3D shape = new Shape3D();
  139.     shape.setGeometry (la);
  140.  
  141.         // use the utility method to set the capabilities
  142.     PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
  143.  
  144.     objTrans.addChild(shape);
  145.       }
  146.     }
  147.  
  148.     // Points
  149.     for (double x=-2.0;x<=2.0;x+=1.0) {
  150.       for (double y=-2.0;y<=2.0;y+=1.0) {
  151.     for (double z=-2.0;z<=2.0;z+=1.0) {
  152.       t3.setTranslation (new Vector3d(-10.0+2.0*x, 0.0+2.0*y,-20.0+2.0*z));
  153.       TransformGroup objTrans = new TransformGroup(t3);
  154.  
  155.       objRoot.addChild(objTrans);
  156.  
  157.       PointArray pa = new PointArray (1, 
  158.                       PointArray.COORDINATES |
  159.                       PointArray.COLOR_3);
  160.  
  161.       pa.setCoordinate (0, new Point3d (0.0, 0.0, 0.0));
  162.       pa.setColor (0, grey);
  163.  
  164.       Shape3D shape = new Shape3D();
  165.       shape.setGeometry (pa);
  166.  
  167.           // use the utility method to set the capabilities
  168.       PickTool.setCapabilities(shape, PickTool.INTERSECT_FULL);
  169.  
  170.       objTrans.addChild(shape);
  171.     }
  172.       }
  173.     }    
  174.  
  175.     return objRoot;
  176.   }
  177.  
  178.   public IntersectTest () {
  179.   }
  180.  
  181.     public void init() {
  182.     setLayout(new BorderLayout());
  183.     
  184.     GraphicsConfiguration config =
  185.         SimpleUniverse.getPreferredConfiguration();
  186.     
  187.     Canvas3D c = new Canvas3D(config);
  188.     add("Center", c);
  189.     
  190.     // Create a simple scene and attach it to the virtual universe
  191.     BranchGroup scene = createSceneGraph();
  192.     u = new SimpleUniverse(c);
  193.     
  194.     // Add picking behavior
  195.     IntersectInfoBehavior behavior =
  196.         new IntersectInfoBehavior (c, scene,0.05f);
  197.     behavior.setSchedulingBounds (bounds);
  198.     scene.addChild (behavior);
  199.     
  200.     TransformGroup vpTrans =
  201.         u.getViewingPlatform().getViewPlatformTransform();
  202.     
  203.     KeyNavigatorBehavior keybehavior = new KeyNavigatorBehavior (vpTrans);
  204.     keybehavior.setSchedulingBounds (bounds);
  205.     scene.addChild (keybehavior);
  206.     scene.setCapability (Group.ALLOW_CHILDREN_EXTEND);
  207.     scene.compile();
  208.     u.addBranchGraph(scene);
  209.     
  210.     View view = u.getViewer().getView();
  211.     view.setBackClipDistance (100000);
  212.     
  213.     }
  214.  
  215.     public void destroy() {
  216.     u.cleanup();
  217.     }
  218.  
  219.   //
  220.   // The following allows IntersectTest to be run as an application
  221.   // as well as an applet
  222.   //
  223.   public static void main(String[] args) {
  224.     String s = "\n\nIntersectTest:\n-----------\n";
  225.     s += "Pick with the mouse over the primitives\n";
  226.     s += "- A sphere will be placed to indicate the picked point.\n";
  227.     s += "If color information is available, the sphere will change color to reflect\n";
  228.     s += "the interpolated color.\n";
  229.     s += "- Other spheres will be placed to show the vertices of the selected polygon\n";
  230.     s += "- Information will be displayed about the picking operation\n\n\n";
  231.  
  232.     System.out.println (s);
  233.  
  234.     new MainFrame(new IntersectTest(), 640, 640);
  235.   }
  236. }
  237.